package com.gxuwz.stats.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.stats.entity.AcademicPapers;
import com.gxuwz.stats.entity.AcademicPapers;
import com.gxuwz.stats.service.AcademicPapersService;
import com.gxuwz.stats.service.AcademicPapersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/academicPapers")
public class AcademicPapersController {

    @Autowired
    private AcademicPapersService academicPapersService;

    @GetMapping("/academicPapersList")
    public ResponseEntity<Map<String, Object>> academicPapersList(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<AcademicPapers> page = new Page<>(currentPage, pageSize);
            QueryWrapper<AcademicPapers> queryWrapper = new QueryWrapper<>();

            // 模糊查询条件
            if (params.containsKey("academicPapersKeyword") && !params.get("academicPapersKeyword").isEmpty()) {
                String keyword = "%" + params.get("academicPapersKeyword") + "%";
                queryWrapper.and(wrapper -> wrapper
                        .like("academicPapersname", keyword)
                        .or().like("role", keyword)
                        .or().like("degree", keyword)
                        .or().like("position", keyword)
                        .or().like("post", keyword)
                        .or().like("department", keyword));
            }

            // 调用 service 层的分页查询方法
            IPage<AcademicPapers> resultPage = academicPapersService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

//    @PostMapping("/importData")
//    public ResponseEntity<Map<String, Object>> importData(
//            @RequestParam("file") MultipartFile file,
//            @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) {
//        try {
//            List<AcademicPapers> academicPapers = academicPapersService.importExcel(file.getInputStream(), updateSupport);
//            Map<String, Object> response = new HashMap<>();
//            response.put("code", 200);
//            response.put("message", "导入成功");
//            response.put("data", academicPapers);
//            return ResponseEntity.ok(response);
//        } catch (Exception e) {
//            Map<String, Object> errorResponse = new HashMap<>();
//            errorResponse.put("code", 400);
//            errorResponse.put("message", "导入失败: " + e.getMessage());
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
//        }
//    }

    /**
     * 新增用户
     */
    @PostMapping("/addAcademicPapers")
    public ResponseEntity<Void> addAcademicPapers(@RequestBody AcademicPapers academicPapers) {

        System.out.println("academicPapers==="+academicPapers);

        academicPapersService.save(academicPapers);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/deleteAcademicPapers")
    public ResponseEntity<Void> deleteAcademicPapers(@RequestBody List<Long> academicPapersIds) {
        System.out.println("删除："+academicPapersIds);
        academicPapersService.removeByIds(academicPapersIds);
        return ResponseEntity.noContent().build();
    }

    /**
     * 修改用户
     */
    @PutMapping("/editAcademicPapers")
    public ResponseEntity<Void> editAcademicPapers(@RequestBody AcademicPapers academicPapers) {
        System.out.println("user::"+academicPapers);
        academicPapersService.updateById(academicPapers);
        return ResponseEntity.ok().build();
    }

}
